package code.example.hystrix.test.impl;

import code.example.tool.LogFactory;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixObservableCommand;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import rx.Observable;
import rx.Subscriber;

import java.io.ByteArrayOutputStream;
import java.util.concurrent.TimeUnit;


public class HystrixVisitHttp  extends HystrixObservableCommand<String> {

     private  String url= null;
     public HystrixVisitHttp(String httpUrl){
         super( HystrixCommandGroupKey.Factory.asKey("Command Group: HTTP"));
         this.url = httpUrl;
     }

     @Override
     protected Observable<String> construct() {

          return Observable.create(new Observable.OnSubscribe<String>() {
               @Override
               public void call(Subscriber<? super String> subscriber) {
                    try {
                         HttpGet httpGet = new HttpGet(url);
                         HttpClient httpClient = HttpClients.createDefault();
                         HttpResponse httpResponse = httpClient.execute(httpGet);
                         ByteArrayOutputStream byteArrayOutputStream = (new ByteArrayOutputStream());
                         httpResponse.getEntity().writeTo(byteArrayOutputStream);
                         String result = new String(byteArrayOutputStream.toByteArray());
                         subscriber.onNext(result);
                         subscriber.onCompleted();
                    }catch (Throwable e){
                         LogFactory.getLog().info("business thread error:");
                         LogFactory.getLog().error(e);
                         subscriber.onError(e);
                    }
               }
          });
     }



    /* public Thread startTest(){
          Thread thread =  new Thread(new Runnable() {
               @Override
               public void run() {
                    try {
                         String result = execute();
                         System.out.println("result:" + result);
                    }catch (Exception e){
                         LogFactory.getLog().info("hystrix thread error:");
                         LogFactory.getLog().error(e);
                    }
               }
          });
          thread.start();
          return thread;

     }*/
     public static void main(String args[]){
          String url="http://www.baidu.com";
          try {
          HystrixVisitHttp hystrixVisitHttp = new HystrixVisitHttp(url);
               Observable<String> observable =  hystrixVisitHttp.toObservable();
               observable.subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
                         LogFactory.getLog().info(" run finish");
                    }

                    @Override
                    public void onError(Throwable e) {
                         LogFactory.getLog().info(" run error");
                    }

                    @Override
                    public void onNext(String s) {
                         LogFactory.getLog().info(" run on next");
                    }
               });
               String result =  observable.toBlocking().toFuture().get();
               LogFactory.getLog().info("main thread result:"+result);
               TimeUnit.MINUTES.sleep(10);
          }catch (Exception e){
               LogFactory.getLog().info("main thread error:");
               LogFactory.getLog().error(e);
          };
     }
}
